﻿namespace Common.Algorithm.Core.LocalTest;

/// <summary>
/// QuantumGeneticExecute.RunMany(5);
/// </summary>
public class QuantumGeneticExecute
{
    public static void Run()
    {
        Console.WriteLine("-------------------------------------------");

        ITargetFunction targetFunction = new HimmelblauFunction( );  //2*16=32

        var func = targetFunction.GenMathFunction();
        QuantumGeneticService quantGeneticAlgm = new QuantumGeneticService(32, 18);
        QuantumGeneticService.targetFunc = func;
        QuantumGeneticService.decodeFunc = targetFunction.BinaryDecode;
        Action action = () =>
        {
            quantGeneticAlgm.Run(120);
        };
        Console.WriteLine($"time:{TimeService.TimeWatch(action)}");
         
        string str = QuantumGeneticService.GetResult();
        var min = func(str.Split(',').Select(s => double.Parse(s)).ToArray());
        Console.WriteLine($"{str}->{min}");
        Console.WriteLine($"{string.Join("", QuantumGeneticService.bestSequence.Select(Q => Q.collapsedValue))}");

    }

    public static void RunMany(int iter)
    {
        Console.WriteLine($"QuantumGenecticAlgorithm \niteration：120 \npopSize:20 ");

        for (int i = 0; i < iter; i++)
        {
            Run();
        }
    }
     
}



